package com.itheima.manager;

import cn.hutool.core.util.StrUtil;
import com.itheima.common.R;
import com.itheima.domain.*;
import com.itheima.service.*;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UmsRoleManager {
    @DubboReference
    IRoleService roleService;
    @DubboReference
    IRoleMenuRelationService roleMenuRelationService;
    @DubboReference
    IRoleResourceRelationService roleResourceRelationService;
    @DubboReference
    IMenuService menuService;
    @DubboReference
    IResourceService resourceService;


    public R<List<Role>> findAll() {
        List<Role> list= roleService.findAll();
        return R.success(list);
    }

    //根据角色名称分页获取角色
    public R<PageBeanVo> findByConditionAndPage(Integer pageNum, Integer pageSize, String keyword) {
        PageBeanVo pageBeanVo=roleService.findByConditionAndPage(pageNum, pageSize, keyword);
        return R.success(pageBeanVo);
    }

    //添加角色
    public R<String> saveRole(Role role) {
        roleService.saveRole(role);
        return R.success("添加成功");
    }

    //给角色分配菜单
    public R<String> updateRoleMenu(String roleId, String menuIds) {
        //先将角色菜单关系表中对应的记录删除掉
        roleMenuRelationService.deleteByRoleId(Long.valueOf(roleId));

        //获取角色对应的新menuId
        List<Long> ids=new ArrayList<>();
        boolean b = menuIds.contains(",");
        if(b){
            String[] split = menuIds.split(",");
            for (String id : split) {
                ids.add(Long.valueOf(id));
            }
        }else{
            ids.add(Long.valueOf(menuIds));
        }
        //将角色id和对应的菜单id加入角色菜单关系表中
        ids.forEach(menuId->{
            RoleMenuRelation roleMenuRelation=new RoleMenuRelation();
            roleMenuRelation.setRoleId(Long.valueOf(roleId));
            roleMenuRelation.setMenuId(menuId);
            roleMenuRelationService.saveRoleMenuRelation(roleMenuRelation);
        });

        return R.success("分配菜单成功");

    }

    public R<String> updateRoleResource(String roleId, String resourceIds) {
        //先将角色资源关系表中对应的记录删除掉
        roleResourceRelationService.deleteByRoleId(Long.valueOf(roleId));

        //获取角色对应的新resourceId
        List<Long> ids=new ArrayList<>();
        boolean b = resourceIds.contains(",");
        if(b){
            String[] split = resourceIds.split(",");
            for (String id : split) {
                ids.add(Long.valueOf(id));
            }
        }else{
            ids.add(Long.valueOf(resourceIds));
        }
        //将角色id和对应的资源id加入角色资源关系表中
        ids.forEach(resourceId->{
            RoleResourceRelation roleResourceRelation=new RoleResourceRelation();
            roleResourceRelation.setRoleId(Long.valueOf(roleId));
            roleResourceRelation.setResourceId(resourceId);
            roleResourceRelationService.saveRoleResourceRelation(roleResourceRelation);
        });

        return R.success("分配资源成功");


    }
    //批量删除角色
    public R<String> deleteByIds(String ids) {
        //获取角色id
        List<Long> roleIds=new ArrayList<>();
        boolean b = ids.contains(",");
        if(b){
            String[] split = ids.split(",");
            for (String id : split) {
                roleIds.add(Long.valueOf(id));
            }
        }else{
            roleIds.add(Long.valueOf(ids));
        }
        roleService.deleteByIds(roleIds);

        return R.success("删除成功");
    }

    //获取角色相关菜单
    public R<List<Menu>> findMenuByRoleId(Long roleId) {

        //根据角色id在角色菜单关系表中查询数据
        List<RoleMenuRelation> roleMenuRelations = roleMenuRelationService.findByRoleId(roleId);

        //遍历集合获取菜单id，去菜单表中查取菜单,并装入集合
        List<Menu> list=new ArrayList<>();
        roleMenuRelations.forEach(roleMenuRelation -> {
            Long menuId = roleMenuRelation.getMenuId();
            Menu menu = menuService.findById(menuId);
            list.add(menu);
        });

        return R.success(list);
    }

    //获取角色相关资源
    public R<List<Resource>> findResourceByRoleId(Long roleId) {
        List<RoleResourceRelation> roleResourceRelations=roleResourceRelationService.findResourceByRoleId(roleId);
        List<Resource> list=new ArrayList<>();
        roleResourceRelations.forEach(roleResourceRelation -> {
            Long resourceId = roleResourceRelation.getResourceId();
            Resource resource = resourceService.findById(resourceId);
            list.add(resource);
        });
        return R.success(list);
    }

    //修改角色
    public R<String> updateById(Role role) {

        roleService.updateRole(role);
        return R.success("修改成功");
    }

    //根据id修改角色状态
    public R<String> updateStatus(Long id, Integer status) {
        //根据id查询角色
        Role role = roleService.findById(id);
        //更改状态
        role.setStatus(status);

        roleService.updateRole(role);

        return R.success("修改成功");
    }


}
